<template>
  <div class="course-item">
    <div class="course-time">
      <div class="start-time">{{ formatTime(course.startTime) }}</div>
      <div class="duration">{{ calculateDuration() }}</div>
    </div>

    <div class="course-info">
      <h4 class="course-title">{{ course.title }}</h4>
      <div class="course-details">
        <div class="detail-item">
          <i class="fas fa-user" aria-hidden="true" />
          <span>{{ course.student }}</span>
        </div>
        <div class="detail-item">
          <i class="fas fa-tag" aria-hidden="true" />
          <span>{{ course.type }}</span>
        </div>
        <div class="detail-item">
          <i class="fas fa-map-marker-alt" aria-hidden="true" />
          <span>{{ course.location }}</span>
        </div>
      </div>

      <div v-if="course.notes" class="course-notes">
        <i class="fas fa-sticky-note" aria-hidden="true" />
        <span>{{ course.notes }}</span>
      </div>
    </div>

    <div class="course-actions">
      <el-button
        size="mini"
        icon="fas fa-edit"
        @click="$emit('edit-course', course)"
      />
      <el-button
        size="mini"
        icon="fas fa-trash-alt"
        class="delete-btn"
        @click="$emit('delete-course', course.id)"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: 'CourseItem',
  props: {
    course: {
      type: Object,
      required: true,
      default: () => ({})
    }
  },
  methods: {
    // 格式化时间显示
    formatTime(date) {
      const d = new Date(date)
      return `${d.getHours().toString().padStart(2, '0')}:${d.getMinutes().toString().padStart(2, '0')}`
    },
    // 计算课程时长
    calculateDuration() {
      const start = new Date(this.course.startTime)
      const end = new Date(this.course.endTime)
      const minutes = (end - start) / (1000 * 60)
      return `${minutes} 分钟`
    }
  }
}
</script>

<style scoped>
.course-item {
  display: flex;
  align-items: flex-start;
  padding: 15px;
  border-radius: 12px;
  background-color: #fff;
  box-shadow: 0 2px 8px rgba(8, 8, 8, 0.08);
  transition: box-shadow 0.3s ease;
}

.course-item:hover {
  box-shadow: 0 4px 16px rgba(8, 8, 8, 0.12);
}

/* 课程时间 */
.course-time {
  width: 80px;
  text-align: center;
  padding-right: 15px;
  border-right: 1px solid #f0f0f0;
  flex-shrink: 0;
}

.start-time {
  font-weight: 500;
  color: #333;
  margin-bottom: 5px;
}

.duration {
  font-size: 12px;
  color: #666;
  background-color: #f0f7ff;
  padding: 2px 6px;
  border-radius: 4px;
  display: inline-block;
}

/* 课程信息 */
.course-info {
  flex: 1;
  padding: 0 15px;
}

.course-title {
  margin: 0 0 10px 0;
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

.course-details {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  margin-bottom: 10px;
}

.detail-item {
  display: flex;
  align-items: center;
  font-size: 13px;
  color: #666;
}

.detail-item i {
  margin-right: 5px;
  color: #0084ff;
  font-size: 12px;
}

.course-notes {
  display: flex;
  align-items: center;
  font-size: 13px;
  color: #666;
  padding-top: 5px;
  border-top: 1px dashed #f0f0f0;
}

.course-notes i {
  margin-right: 5px;
  color: #0084ff;
  font-size: 12px;
}

/* 课程操作按钮 */
.course-actions {
  display: flex;
  gap: 5px;
  flex-shrink: 0;
}

.course-actions .el-button {
  padding: 4px 8px;
  border-radius: 6px;
}

.delete-btn {
  color: #ff4d4f;
  border-color: #ffccd5;
  background-color: #fff2f2;
}
</style>
